﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Framework;
using DataAccess;

namespace DomMgmtSystem.View
{
    public partial class UserAdd : System.Web.UI.Page
    {
        #region 属性
        /// <summary>
        /// 
        /// </summary>
        private decimal NID
        {
            get
            {
                if (!string.IsNullOrEmpty(Request["NID"]))
                    return Convert.ToDecimal(Request["NID"]);
                return 0;
            }
        }

        /// <summary>
        /// 是否是添加
        /// </summary>
        public bool IsNew
        {
            get
            {
                if (ViewState["IsNew"] != null)
                    return Convert.ToBoolean(ViewState["IsNew"]);
                else
                    return this.GetRequestStr("isnew") != string.Empty;
            }

            set
            {
                ViewState["IsNew"] = value;
            }
        }

        /// <summary>
        /// 是否是浏览
        /// </summary>
        public bool IsView
        {
            get
            {
                return this.GetRequestStr("view") != string.Empty;
            }

        }

        #endregion
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (CommonCode.CurUser.NTYPE != 1)
                {
                    Response.Write("您当前没有权限访问该页面");
                    this.Controls.Clear();
                }
                DataBind();
            }

            if (Request.Params["view"] != null)
                ClientScript.RegisterStartupScript(typeof(Page), "a", "<script>ChangeToLabel();</script>");
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {

                if (TB_USER.Exists(txtUserName.Text.Trim()))
                {
                    this.Alert("该登录名已存在，请更改");
                    return;
                }
                Model.TB_USER model = new Model.TB_USER();
                if (NID != 0)
                    model = DataAccess.TB_USER.GetModel(NID);

                model.CNICKNAME = txtNickName.Text.Trim();
                model.CUSERNAME = txtUserName.Text.Trim();
                model.CPASSWORD = CommonCode.DEFAULTPWD;
                model.NTYPE = ddlType.SelectedValue.ToInt();

                if (NID != 0)
                {
                    if (TB_USER.Update(model))
                        this.RedirectAfterInfo("修改成功！", "UserList.aspx");
                    else
                        this.Alert("修改失败！");
                }
                else
                    if (TB_USER.Add(model))
                    { this.RedirectConfirmInfo("新增成功！是否继续新增？", "UserAdd.aspx", "UserList.aspx"); }
                    else
                    { this.Alert("保存失败！"); }

            }
            catch (Exception ex)
            {
                LogHelper.ErrorFormat("新增用户保存异常:{0}{1}", ex.Message, ex.StackTrace);
                this.Alert("保存异常！");
            }
        }

        public void DataBind()
        {
            if (NID != 0)
            {
                Model.TB_USER model = DataAccess.TB_USER.GetModel(NID);
                if (model != null)
                {
                    txtUserName.Text = model.CUSERNAME.ToString();
                    txtNickName.Text = model.CNICKNAME;
                    ddlType.SelectedIndex = model.NTYPE.Value.ToString().ToInt();
                }
                btnBack.Visible = true;
                if (Request.Params["view"] != null)
                    lbTitle.Text = "查看用户";
                else
                    lbTitle.Text = "编辑用户";
            }
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserList.aspx");
        }
    }
}